package com.example.test;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.test.demin.Bill;
import com.example.test.utils.DbDaoUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

public class BillListActivity extends BaseActivity implements OnClickListener {

	@ViewInject(R.id.txt_title)
	private TextView txt_title;
	@ViewInject(R.id.imgbtn_left)
	private ImageView imgbtn_left;
	@ViewInject(R.id.rl_tiltebar_left)
	private RelativeLayout rl_left;
	@ViewInject(R.id.rl_tiltebar_right)
	private RelativeLayout rl_right;
	@ViewInject(R.id.imgbtn_right)
	private ImageView imagebtn_right;
	private String title;
	@ViewInject(R.id.bill_list)
	private ListView billlist;
	private List<Bill> list;
	private ListAdapter adapter;
	private DbDaoUtils dao;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mTintManager.setStatusBarTintEnabled(true);
		mTintManager.setStatusBarTintResource(R.color.darkwhite);
		setContentView(R.layout.activity_billlist);
		ViewUtils.inject(this);
		txt_title.setText("历史账单");
		
		imgbtn_left.setBackgroundResource(R.drawable.title_back);
		imagebtn_right.setVisibility(View.INVISIBLE);
		rl_left.setOnClickListener(this);
		getlist();
		billlist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				int ids= list.get(position).get_id();
				 Intent intent=new Intent(BillListActivity.this,BilldetailActivity.class);
				 intent.putExtra("fromwhat", "billlist");
				 intent.putExtra("ids", ids);
				 startActivity(intent);
			}
		});
	}

	private void getlist() {
		dao = new DbDaoUtils(this);
		list = dao.findALLBillInfo();
		if (adapter == null) {
			adapter = new ListAdapter();
			billlist.setAdapter(adapter);
			
		} else {
			adapter.notifyDataSetChanged();
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getlist();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.rl_tiltebar_left:
			this.finish();
			break;

		default:
			break;
		}
	}

	class ListAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (convertView != null) {
				holder = (ViewHolder) convertView.getTag();
			} else {
				convertView = View.inflate(BillListActivity.this,
						R.layout.list_billitem, null);
				holder = new ViewHolder();
				holder.tv_cost = (TextView) convertView
						.findViewById(R.id.tv_cost);
				holder.tv_billnumber = (TextView) convertView
						.findViewById(R.id.tv_billnumber);
				holder.tv_billtime = (TextView) convertView
						.findViewById(R.id.tv_billtime);
				convertView.setTag(holder);
			}
			holder.tv_cost
					.setText(String.valueOf(list.get(position).getCost()));
			holder.tv_billnumber.setText(list.get(position).getBillnumber());
			holder.tv_billtime.setText(list.get(position).getTime());
			// int i=position%ids2.length;
			// if(i>=6){
			// i=6;
			// }
			// holder.ly_card.setBackgroundResource(ids2[i]);
			return convertView;
		}

	}

	public static class ViewHolder {
		TextView tv_cost;
		TextView tv_billnumber;
		TextView tv_billtime;
	}
}
